#!/bin/sh
#
# Startup script for the edna mp3 mini server 
#
# chkconfig:2345 97 97
# description: edna is a mini mp3 webserver 
#
# processname: python
# pidfile: /var/run/ednad.pid
# config: 

# Source function library.

#debian vars
EDNA=/usr/bin/edna
EDNACONF=/etc/edna/edna.conf
PIDFILE=/var/run/edna.pid
# redhat
EDNAD=/usr/bin/ednad
initdir=/etc/init.d
pidfile=/var/run/ednad.pid

PYTHON=`which python`
PYTHONPATH=/usr/lib/edna
export PYTHONPATH
#
# Determine which kind of configuration we're using
#
system=unknown
if [ -f /etc/debian_version ]; then
    system=debian
elif [ -f /etc/redhat-release ]; then
    system=redhat
elif [ -f /etc/SuSE-release ]; then
    system=suse
else
    echo "$0: Unknown system, please port" 1>&2
    exit 1
fi

if [ $system = redhat ]; then
    . $initdir/functions
fi

if [ $system = suse ]; then
    . /etc/rc.status
fi

[ -f $EDNA ] || exit 0


RETVAL=0

function suse () {
    case "$1" in
    start)
        echo -n "Starting edna: "
        startproc -p $PIDFILE $PYTHON $EDNA $EDNACONF >/dev/null 2>&1
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down edna: "
	killproc -p $PIDFILE $PYTHON $EDNA $EDNACONF >/dev/null 2>&1
        rc_status -v
        ;;
    status)
        echo -n "Checking for service edna: "
        checkproc -p $PIDFILE $PYTHON $EDNA $EDNACONF
        rc_status -v
        ;;
    restart)
        $0 stop
        $0 start
        rc_status
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
    esac
}

function redhat () {
    case "$1" in
    start)
        echo -n "Starting ednad: "
        daemon ${EDNAD}
        echo
        touch /var/lock/subsys/ednad
        return $RETVAL
        ;;
    stop)
        echo -n "Shutting down ednad: "
        EDNADPID=
        EDNADPID=$(cat $pidfile)
        kill $EDNADPID
        echo
        rm -f /var/lock/subsys/ednad $pidfile
        return $RETVAL
        ;;
    status)
        EDNADPID=
        EDNADPID=$(cat $pidfile)
        echo -n "Checking for ednad, PID is : " $EDNADPID
        echo 
         ;;
    restart)
         stop
         start
         ;;
    *)
        echo "Usage: $0 {start | stop | restart | status}"
	exit 1
    esac
}

function debian () {
case "$1" in
    start)
	echo -n 'Starting edna mp3 server: edna'
	start-stop-daemon --start --quiet --pidfile $PIDFILE  --exec $EDNA -- --daemon $EDNACONF
        echo "."
        ;;

    stop)
	echo -n 'Stopping edna mp3 server: edna'
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        echo "."
	;;

    reload)
        start-stop-daemon --stop --signal 1 --oknodo --pidfile $PIDFILE
        ;;


  restart)
	echo -n 'Restarting edna mp3 server: edna'
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE 
	sleep 10
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $EDNA -- --daemon $EDNACONF
	echo "."
	;;

  *)
	echo "Usage: /etc/init.d/edna {start|stop|reload|restart}"
	exit 1
esac
}

if [ $system = debian ]; then
	debian "$@"
elif [ $system = redhat ]; then
	redhat "$@"
elif [ $system = suse ]; then
        suse "$@"
fi


exit 0
